#include "RasterizerState.h"


namespace Display
{
	RasterizerState::RasterizerState()
	{
		Display::DeviceUserBase();

		state = nullptr;
		ZeroMemory(&desc,sizeof(D3D11_RASTERIZER_DESC));	
	}

	RasterizerState::~RasterizerState()
	{
		Dispose();
	}
	void RasterizerState::Dispose()
	{
		if(state != nullptr)
		{
			state->Release();
			state = nullptr;
		}
	}

	void RasterizerState::Build()
	{
		desc.AntialiasedLineEnable = false;

		if(cullMode == FRONT)
			desc.CullMode = D3D11_CULL_FRONT;
		else if(cullMode == BACK)
			desc.CullMode = D3D11_CULL_BACK;
		else desc.CullMode = D3D11_CULL_NONE;

		desc.DepthBias = 0;
		desc.DepthBiasClamp = 0.0f;
		desc.DepthClipEnable = true;

		if(fillMode == SOLID)
			desc.FillMode = D3D11_FILL_SOLID;
		else desc.FillMode = D3D11_FILL_WIREFRAME;

		desc.FrontCounterClockwise = false;
		desc.MultisampleEnable = false;
		desc.ScissorEnable = false;
		desc.SlopeScaledDepthBias = 0.0f;

		
		ID3D11Device * device = GetDevice();
		

		device->CreateRasterizerState(&desc,&state);

		device = nullptr;
	}

	void RasterizerState::SubmitChanges()
	{
		Dispose();
		Build();
	}

	void RasterizerState::Apply()
	{
		if(state != nullptr)
		{
			ID3D11DeviceContext * context = GetContext();
			context->RSSetState(state);
			context = nullptr;
		}
	}

	RasterizerState * RasterizerState::Create(ID3D11Device * device)
	{
		RasterizerState * state = new RasterizerState();
		state->SetDevice(device);

		state->SetFillMode(SOLID);
		state->SetCullMode(NONE);

		return state;
	}

	void RasterizerState::Destroy(RasterizerState * state)
	{
		if(state != nullptr)
		{
			delete state;
			state = nullptr;
		}
	}

	void RasterizerState::SetFillMode(FillMode mode)
	{
		fillMode = mode;
	}
	FillMode RasterizerState::GetFillMode()
	{
		return fillMode;
	}
		
	void RasterizerState::SetCullMode(CullMode mode)
	{
		cullMode = mode;
	}
	CullMode RasterizerState::GetCullMode()
	{
		return cullMode;
	}
}